package web_hr;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CheckDay
 */
@WebServlet("/CheckDay")
public class CheckDay extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckDay() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String day = request.getParameter("day");
		String name = request.getParameter("name");
		Model dayTime = new Model("hr_time");
		//组装今日时间
		Calendar calendar = Calendar.getInstance();
		int mon = calendar.get(Calendar.MONTH)+1; 
		int year = calendar.get(Calendar.YEAR); 
		String theDay = null;
		if(mon<10){
			theDay = year+"-0"+mon+"-"+day;
		}else{
			theDay = year+"-"+mon+"-"+day;
		}
		String search = "select (to_char(end,'HH24:MI:SS'))sta,(to_char(end,'HH24:MI:SS'))end from hr_time where sta between to_date('"+theDay+" 00:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('"+theDay+" 23:59:59','YYYY-MM-DD HH24:MI:SS') and username='"+name+"'";
		ResultSet rs = dayTime.eQuery(search);
		PrintWriter pw = response.getWriter();
		String str = null;
		try {
			if(rs.next()){
				str ="sign in: "+rs.getString(1)+" sign out: "+rs.getString(2);
			}else{
				str = "no sign in and sign out";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dayTime.close();
		}
		pw.print(str);
	}

}
